package bera.lee.memorycard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 *<pre>
 * <b><font color="blue">MapUtil</font></b>
 * </pre>
 * 
 *<pre>
 * <b>&nbsp;--描述说明--</b>
 * </pre>
 * 
 * <pre>
 * 生成地图的辅助类
 * </pre>
 * 
 * <pre>
 * <b>--样例--</b>
 *   MapUtil obj = new MapUtil();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * @author <b>beralee</b>
 */
public class MapUtil {
	public static final int[] mario = new int[] { R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4,
			R.drawable.c5 };

	public static final int[] fuwa = new int[] { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4,
			R.drawable.b5, R.drawable.b6 };

	public static final int[] naruto = new int[] { R.drawable.n1, R.drawable.n2, R.drawable.n3, R.drawable.n4,
			R.drawable.n5, R.drawable.n6, R.drawable.n7 };

	public static final int[] flower = new int[] { R.drawable.f1, R.drawable.f2, R.drawable.f3, R.drawable.f4,
			R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8 };

	public static final int[] face = new int[] { R.drawable.fa1, R.drawable.fa2, R.drawable.fa3, R.drawable.fa4,
			R.drawable.fa5, R.drawable.fa6, R.drawable.fa7, R.drawable.fa8, R.drawable.fa9 };

	public static final int[] eye = new int[] { R.drawable.e1, R.drawable.e2, R.drawable.e3, R.drawable.e4,
			R.drawable.e5, R.drawable.e6, R.drawable.e7, R.drawable.e8, R.drawable.e9, R.drawable.e10,
			R.drawable.e11, R.drawable.e12 };

	public static final int[] car = new int[] { R.drawable.ct1, R.drawable.ct2, R.drawable.ct3, R.drawable.ct4,
			R.drawable.ct5, R.drawable.ct6, R.drawable.ct7, R.drawable.ct8, R.drawable.ct9, R.drawable.ct10,
			R.drawable.ct11, R.drawable.ct12, R.drawable.ct13, R.drawable.ct14 };

	public static final int[] pre = new int[] { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
			R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10,
			R.drawable.p11, R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15, R.drawable.p16 };

	public static final int[] tiaozhan = new int[] { R.drawable.ct14, R.drawable.ct5, R.drawable.p13,
			R.drawable.p14, R.drawable.p7, R.drawable.p11, R.drawable.e4, R.drawable.e5, R.drawable.e11,
			R.drawable.fa1, R.drawable.fa2, R.drawable.fa3, R.drawable.f1, R.drawable.f2, R.drawable.f3,
			R.drawable.n4, R.drawable.n7, R.drawable.b1, R.drawable.b2, R.drawable.c4 };

	public static final int[] card9 = new int[] { R.drawable.ny1, R.drawable.ny2, R.drawable.ny3, R.drawable.ny4,
			R.drawable.ny5, R.drawable.ny6, R.drawable.ny7, R.drawable.ny8, R.drawable.ny9, R.drawable.ny10,
			R.drawable.ny11, R.drawable.ny12, R.drawable.ny13 };

	public static final int[] card10 = new int[] { R.drawable.t1, R.drawable.t2, R.drawable.t3, R.drawable.t4,
			R.drawable.t5, R.drawable.t6, R.drawable.t7, R.drawable.t8, R.drawable.t9, R.drawable.t10,
			R.drawable.t11, R.drawable.t12, R.drawable.t13, R.drawable.t14, R.drawable.t15, R.drawable.t16,
			R.drawable.t17, R.drawable.t18, R.drawable.t19, R.drawable.t20, R.drawable.t21 };

	public static final int[] card11 = new int[] { R.drawable.kt1, R.drawable.kt2, R.drawable.kt3, R.drawable.kt4,
			R.drawable.kt5, R.drawable.kt6, R.drawable.kt7, R.drawable.kt8, R.drawable.kt9, R.drawable.kt10,
			R.drawable.kt11, R.drawable.kt12, R.drawable.kt13, R.drawable.kt14, R.drawable.kt15, R.drawable.kt16,
			R.drawable.kt17, R.drawable.kt18, R.drawable.kt19, R.drawable.kt20, R.drawable.kt21, R.drawable.kt22,
			R.drawable.kt23, R.drawable.kt24 };

	public static final int[] card12 = new int[] { R.drawable.nb1, R.drawable.nb2, R.drawable.nb3, R.drawable.nb4,
			R.drawable.nb5, R.drawable.nb6, R.drawable.nb7, R.drawable.nb8, R.drawable.nb9, R.drawable.nb10,
			R.drawable.nb11, R.drawable.nb12, R.drawable.nb13, R.drawable.nb14, R.drawable.nb15, R.drawable.nb16,
			R.drawable.nb17, R.drawable.nb18, R.drawable.nb19, R.drawable.nb20, R.drawable.nb21, R.drawable.nb22,
			R.drawable.nb23, R.drawable.nb24 };

	public static final List initalldatalist = new ArrayList();

	public static final HashMap<String, int[]> map_cardtype = new HashMap<String, int[]>();
	static {
		map_cardtype.put("mario", mario);
		map_cardtype.put("fuwa", fuwa);
		map_cardtype.put("naruto", naruto);
		map_cardtype.put("flower", flower);
		map_cardtype.put("face", face);
		map_cardtype.put("eye", eye);
		map_cardtype.put("pre", pre);
		map_cardtype.put("car", car);
		map_cardtype.put("card9", card9);
		map_cardtype.put("card10", card10);
		map_cardtype.put("card11", card11);
		map_cardtype.put("card12", card12);
		map_cardtype.put("tiaozhan", tiaozhan);

	}

	public static int[] createTiaoZhanMap() {
		List<Integer> initlist = new ArrayList<Integer>();
		initlist = MapUtil.addArrayToList(mario, initlist);
		initlist = MapUtil.addArrayToList(fuwa, initlist);
		initlist = MapUtil.addArrayToList(naruto, initlist);
		initlist = MapUtil.addArrayToList(flower, initlist);
		initlist = MapUtil.addArrayToList(face, initlist);
		initlist = MapUtil.addArrayToList(eye, initlist);
		initlist = MapUtil.addArrayToList(pre, initlist);
		initlist = MapUtil.addArrayToList(car, initlist);
		initlist = MapUtil.addArrayToList(card9, initlist);
		initlist = MapUtil.addArrayToList(card10, initlist);
		initlist = MapUtil.addArrayToList(card11, initlist);
		initlist = MapUtil.addArrayToList(card12, initlist);
		int[] result = new int[24];
		for (int i = 0; i < 24; i++) {
			Random ran = new Random();
			int index = ran.nextInt(initlist.size() - 1);
			result[i] = initlist.get(index);
			initlist.remove(index);
		}
		return result;
	}

	public static List<Integer> addArrayToList(int[] foraddarray, List<Integer> foraddlist) {
		for (int i = 0; i < foraddarray.length; i++) {
			foraddlist.add(new Integer(foraddarray[i]));
		}
		return foraddlist;
	}

	/**
	 * 创建地图
	 * 
	 * @param cardsize
	 *            总卡片个数
	 * @param point
	 *            卡片类别
	 * @return
	 * @author:beralee
	 */
	public static int[] createMap(int cardnum, String cardtype) {

		int[] initvalues = new int[cardnum];
		int[] typedata = null;
		if (cardtype.equals("tiaozhan")) {
			typedata = createTiaoZhanMap();
		} else {
			typedata = map_cardtype.get(cardtype);
		}
		int index = 0;
		boolean twice = false;
		for (int i = 0; i < initvalues.length; i++) {
			initvalues[i] = typedata[index];
			if (twice) {
				index++;
				twice = false;
			} else {
				twice = true;
			}
			if (index > typedata.length - 1) {
				index = 0;
			}
		}
		return luan(initvalues);
	}

	/**
	 * 根据关卡设定状态
	 * 
	 * 
	 * @return
	 * @author:beralee
	 */
	public static void configByPoint() {
		switch (Config.Point) {
		case 1:
			Config.CARD_NUM = 12;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "fuwa";
			Config.DCARD = R.drawable.icon;
			break;
		case 2:
			Config.CARD_NUM = 16;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "mario";
			Config.DCARD = R.drawable.icon2;
			break;
		case 3:
			Config.CARD_NUM = 20;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "naruto";
			Config.DCARD = R.drawable.icon3;
			break;
		case 4:
			Config.CARD_NUM = 24;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "flower";
			Config.DCARD = R.drawable.icon;
			break;
		case 5:
			Config.CARD_NUM = 30;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "face";
			Config.DCARD = R.drawable.icon2;
			break;
		case 6:
			Config.CARD_NUM = 36;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "eye";
			Config.DCARD = R.drawable.icon3;
			break;
		case 7:
			Config.CARD_NUM = 42;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "car";
			Config.DCARD = R.drawable.icon;
			break;
		case 8:
			Config.CARD_NUM = 48;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "pre";
			Config.DCARD = R.drawable.icon2;
			break;
		case 9:
			Config.CARD_NUM = 30;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "card9";
			Config.DCARD = R.drawable.icon3;
			break;
		case 10:
			Config.CARD_NUM = 48;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "card10";
			Config.DCARD = R.drawable.icon;
			break;
		case 11:
			Config.CARD_NUM = 48;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "card11";
			Config.DCARD = R.drawable.icon2;
			break;
		case 12:
			Config.CARD_NUM = 48;
			Config.BACKGROUND = R.drawable.bg7;
			Config.CARD_TYPE = "card12";
			Config.DCARD = R.drawable.icon3;
			break;
		}
	}

	/**
	 * 乱序
	 * 
	 * @param cardvalus
	 * @return
	 * @author:beralee
	 */
	public static int[] luan(int[] cardvalus) {
		int[] seed = new int[cardvalus.length];
		for (int k = 0; k < cardvalus.length; k++) {
			seed[k] = cardvalus[k];
		}
		int[] ranArr = new int[cardvalus.length];
		Random ran = new Random();
		for (int i = 0; i < seed.length; i++) {
			int j = ran.nextInt(seed.length - i);
			ranArr[i] = seed[j];
			seed[j] = seed[seed.length - 1 - i];
		}
		return ranArr;
	}
}
